package com.foreverwin.mes.lims.service.impl;

import com.foreverwin.mes.core.base.FrontPage;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.lims.model.SourceDataRecord;
import com.foreverwin.mes.lims.mapper.SourceDataRecordMapper;
import com.foreverwin.mes.lims.service.SourceDataRecordService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Levy
 * @since 2020-03-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SourceDataRecordServiceImpl extends ServiceImpl<SourceDataRecordMapper, SourceDataRecord> implements SourceDataRecordService {


    @Autowired
    private SourceDataRecordMapper sourceDataRecordMapper;

    @Override
    public Page<SourceDataRecord> selectPage(FrontPage<SourceDataRecord> frontPage, SourceDataRecord sourceDataRecord) {
        EntityWrapper<SourceDataRecord> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(sourceDataRecord);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<SourceDataRecord> selectList(SourceDataRecord sourceDataRecord) {
        EntityWrapper<SourceDataRecord> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(sourceDataRecord);
        return super.selectList(queryWrapper);
    }

    @Override
    public void doSave(String site, String taskNo, Integer total, String user, List<Map> maps) {
        Date nowDate = new Date();


        for (int i=0;i<maps.size();i++){
            SourceDataRecord Model = new SourceDataRecord();
            Model.setInspectParam( (String) maps.get( i ).get("inspectionParam") );
            Model.setSite( site );
            Model.setTaskId( taskNo );
            SourceDataRecord sourceDataRecordModel = sourceDataRecordMapper.selectOne( Model );
            if ( sourceDataRecordModel != null ){
                throw BusinessException.build("源数据已经维护!");
            }
            if (StringUtils.isBlank( (String) maps.get( i ).get("inspectionParam") )){
                continue;
            }

            Integer standardMax = (Integer)maps.get( i ).get("standardMax") ;
            Integer standardMin = (Integer)maps.get( i ).get("standardMin") ;
            SourceDataRecord sourceDataRecord = new SourceDataRecord();
            sourceDataRecord.setSite( site );
            sourceDataRecord.setSeq(  String.valueOf( i ) );
            sourceDataRecord.setCreatedUser( user );
            sourceDataRecord.setCreatedDateTime( nowDate );
            sourceDataRecord.setInspectParam( (String) maps.get( i ).get("inspectionParam") );
            sourceDataRecord.setInspectStandard( (String) maps.get( i ).get("standard") );
            sourceDataRecord.setParam( (String) maps.get( i ).get("paramName") );
            sourceDataRecord.setStandardMax( new BigDecimal( standardMax ) );
            sourceDataRecord.setStandardMin( new BigDecimal( standardMin ) );
            sourceDataRecord.setTaskId( taskNo );
            sourceDataRecord.setSourceData( new BigDecimal( total ) );
            sourceDataRecord.setHandle(UUID.randomUUID().toString());
            sourceDataRecordMapper.insert( sourceDataRecord );

        }


    }
}